package com.neoCalf.neoCommon.utils;

import com.neoCalf.neoCommon.enums.ResultCode;
import com.neoCalf.neoCommon.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Properties;


@Slf4j
public class MailUtils {
    private static final String USER = "2481771560@qq.com";
    private static final String PASSWORD = "nlomcfrxaqojdjgj";
    private static final String TEMPLATE = "E:\\neoCalf\\neo-common\\src\\main\\resources\\templates\\email.html";

    /**
     * 发送邮件
     *
     * @param to    收件人邮箱
     * @param text  邮件正文
     * @param title 邮件标题
     * @return boolean【result】
     */
    public static boolean sendMail(String to, String text, String title) {
        try {
            final Properties pro = new Properties();
            pro.put("mail.smtp.auth", "true");
            pro.put("mail.smtp.starttls.enable", "true"); // 开启 TLS
            pro.put("mail.smtp.ssl.enable", "true"); // 开启 SSL
            pro.put("mail.smtp.host", "smtp.qq.com");
            pro.put("mail.smtp.port", "465"); // 或 587
            //发件人账户
            pro.put("mail.user", USER);
            //发件人密码
            pro.put("mail.password", PASSWORD);

            //构建授权信息, 用于进行SMTP身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication(USER, PASSWORD);
                }
            };
            //使用环境属性和授权信息, 创建邮件回话
            Session AuthSession = Session.getInstance(pro, authenticator);

            //创建邮箱信息
            MimeMessage message = new MimeMessage(AuthSession);

            //添加邮件标题
            message.setSubject(title);

            //添加发件人
            InternetAddress form = new InternetAddress(USER);
            message.setFrom(form);

            //添加收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            //添加邮件内容
            message.setContent(text, "text/html;charset=UTF-8");

            //发送邮件
            Transport.send(message);
            return true;
        } catch (Exception e) {
            log.error(String.valueOf(e));
            return false;
        }
    }

    public static void sendMailCheck(String to, String check) {
        String text = readHtml(TEMPLATE, check);
        boolean b = sendMail(to, text, "neoCalf平台验证码");
        if (!b) {
            throw new BusinessException(ResultCode.CODE_10018);
        }
    }

    public static String readHtml(String filePath, String check) {
        StringBuilder htmlContent = new StringBuilder();
        try {
            FileInputStream fip = new FileInputStream(filePath);
            InputStreamReader reader = new InputStreamReader(fip, StandardCharsets.UTF_8);
            BufferedReader bufferedReader = new BufferedReader(reader);
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                if ("<span class=\"email\"></span>".equals(line.trim())) {
                    line = "<span class=\"email\">" + check + "</span>";
                }
                htmlContent.append(line).append("\n");
            }
            bufferedReader.close();
            reader.close();
            fip.close();
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return htmlContent.toString();
    }
}
